package cn.bfay.lion.jingdong.controller;

import cn.bfay.lion.jingdong.param.AddProductParam;
import cn.bfay.lion.jingdong.service.JfService;
import cn.bfay.lion.jingdong.service.ProductDetailInfoService;
import cn.bfay.lion.jingdong.service.ProductInfoService;
import cn.bfay.lion.jingdong.vo.GeneralPromotionLinkVo;
import cn.bfay.lion.jingdong.vo.ProductDetailInfoVo;
import cn.bfay.lion.jingdong.vo.ProductInfoVo;
import cn.bfay.web.result.ListResult;
import cn.bfay.web.result.PageResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * JingdongController.
 *
 * @author wangjiannan
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/jingdong/product")
public class JingdongController {
    private final ProductInfoService productInfoService;
    private final ProductDetailInfoService productDetailInfoService;
    private final JfService jfService;

    /**
     * 添加商品.
     *
     * @param addProductParam addProductParam
     */
    @PostMapping("/add-product-by-skuid")
    public void addProductBySkuid(@Valid @RequestBody AddProductParam addProductParam) {
        productInfoService.addProductBySkuid(addProductParam.getSkuid());
    }

    /**
     * 查找商品-一个.
     *
     * @param skuid skuid
     * @return {@link ProductInfoVo}
     */
    @GetMapping("/get-product-info-by-skuid")
    public ProductInfoVo getProductInfoBySkuid(@RequestParam("skuid") String skuid) {
        return productInfoService.getProductBySkuid(skuid);
    }

    /**
     * 查找商品-多个.
     *
     * @param skuids skuid串，多个以','分隔
     * @return {@link ProductInfoVo}
     */
    @GetMapping("/get-product-info-by-skuids")
    public ListResult<ProductInfoVo> getProductInfoBySkuids(@RequestParam("skuids") String skuids) {
        return new ListResult<>(productInfoService.getProductBySkuids(skuids));
    }

    /**
     * 查找商品-分页.
     *
     * @param name     参数
     * @param pageNum  pageNum
     * @param pageSize pageSize
     * @return {@link ProductInfoVo}
     */
    @GetMapping("/get-product-info-by-paging")
    public PageResult<ProductInfoVo> getProductInfoByPaging(@RequestParam(value = "name", required = false) String name,
                                                            @RequestParam("pageNum") Long pageNum,
                                                            @RequestParam("pageSize") Long pageSize) {
        return productInfoService.getProductByPaging(name, pageNum, pageSize);
    }

    /**
     * 商品详情数据.
     *
     * @param skuid skuid
     * @return {@link ProductDetailInfoVo}
     */
    @GetMapping("/get-product-detail-infos")
    public ListResult<ProductDetailInfoVo> getProductDetailInfos(@RequestParam("skuid") String skuid) {
        return new ListResult<>(productDetailInfoService.getProductDetailInfos(skuid));
    }

    /**
     * 获取通用推广链接.
     *
     * @param skuid skuid
     * @return {@link GeneralPromotionLinkVo}
     */
    @GetMapping("/get-promotion-common")
    public GeneralPromotionLinkVo getPromotionCommon(@RequestParam("skuid") String skuid) {
        return jfService.getPromotionCommon(skuid);
    }
}